package org.raymond.iworks.study.orders.repository;

import org.raymond.iworks.study.orders.entity.TInventoriesSegments;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TInventorySegmentsRepository extends JpaRepository<TInventoriesSegments, Long> {

    @Query(value = "select count(*) from t_inventories_segments " +
            " where product_id=?1 and segment_usable_num>0 limit 1",
            nativeQuery = true)
    Long countByProductId(Long productId);
    @Modifying
    @Query(value = "update t_inventories_segments set " +
            " segment_usable_num=segment_usable_num-?3, " +
            " segment_lock_num=segment_lock_num+?3 " +
            " where product_id=?1 and segment_lock=?2 and segment_usable_num>0",
            nativeQuery = true)
    int updateInventoriesSegment(Long productId, String segmentLock, Integer num);

    List<TInventoriesSegments> findByProductIdAndSegmentUsableNumGreaterThan(Long productId, Integer segmentUsableNum);
}
